#!/bin/bash
# Copyright (c) 2024 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -e
set +e
echo -e "\n\033[32m\t*********start to regenerate packages !*********\033[0m\n"
echo -e "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
function check_shell_environment() {
  case $(uname -s) in 
    Linux)
          shell_result=$(/bin/sh -c 'echo ${BASH_VERSION}')
          if [ -n "${shell_result}" ]; then
            echo -e "\033[32mSystem shell: bash ${shell_result}\033[0m"
          else
            echo -e "\033[33m Your system shell isn't bash, we recommend you to use bash, because some commands may not be supported in other shells, such as pushd and shopt are not supported in dash. \n You can follow these tips to modify the system shell to bash on Ubuntu: \033[0m"
            echo -e "\033[33m [1]:Open the Terminal tool and execute the following command: sudo dpkg-reconfigure dash \n [2]:Enter the password and select <no>  \033[0m"
          fi
          ;;
    Darwin)
          echo -e "\033[31m[OHOS ERROR] Darwin system is not supported yet\033[0m"
          ;;
    *)
          echo -e "\033[31m[OHOS ERROR] Unsupported this system: $(uname -s)\033[0m"
          exit 1
  esac
}

check_shell_environment 

WORK_DIR=$(cd $(dirname $0); pwd)
OUT_DIR="${WORK_DIR}/out/rk3568"

cd $OUT_DIR

f2fs_path="${OUT_DIR}/clang_x64/thirdparty/f2fs-tools"
if [ -f "$f2fs_path/fsck.f2fs" ]; then
    rm $f2fs_path/resize.f2fs
    ln -s $f2fs_path/fsck.f2fs $f2fs_path/resize.f2fs
    rm $f2fs_path/sload.f2fs
    ln -s $f2fs_path/fsck.f2fs $f2fs_path/sload.f2fs
else
    echo "文件 $f2fs_path/fsck.f2fs不存在"
fi

chmod +x -R ${OUT_DIR}/clang_x64/thirdparty

echo "the current dir is "$(pwd) 

echo "The third step: begin to regenerate img files, please wait ..."

echo "update chip_ckm.img in packages/phone/images"
../../build/ohos/images/mkimage/mkchip_ckm.py --src-dir packages/phone/chip_ckm --device-name packages/phone/images/chip_ckm.img --config-file-path ../../build/ohos/images/mkimage/chip_ckm.txt --mkextimage-tools-path ../../build/ohos/images/mkimage/mkextimage.py --build-image-tools-path clang_x64/thirdparty/f2fs-tools clang_x64/thirdparty/e2fsprogs ../../third_party/e2fsprogs/prebuilt/host/bin

echo "update chip_prod.img in packages/phone/images"
../../build/ohos/images/build_image.py --depfile gen/build/ohos/images/phone_chip_prod_image.d --image-name chip_prod --input-path packages/phone/chip_prod --image-config-file ../../build/ohos/images/mkimage/chip_prod_image_conf.txt --device-image-config-file packages/imagesconf/chip_prod_image_conf.txt --output-image packages/phone/images/chip_prod.img --target-cpu arm --build-variant root --build-image-tools-path clang_x64/thirdparty/e2fsprogs clang_x64/thirdparty/f2fs-tools ../../third_party/e2fsprogs/prebuilt/host/bin ../../build/ohos/images/mkimage

echo "update eng_chipset.img in packages/phone/images"
../../build/ohos/images/build_image.py --depfile gen/build/ohos/images/phone_eng_chipset_image.d --image-name eng_chipset --input-path packages/phone/eng_chipset --image-config-file ../../build/ohos/images/mkimage/eng_chipset_image_conf.txt --device-image-config-file packages/imagesconf/eng_chipset_image_conf.txt --output-image packages/phone/images/eng_chipset.img --target-cpu arm --build-variant root --build-image-tools-path clang_x64/thirdparty/e2fsprogs clang_x64/thirdparty/f2fs-tools ../../third_party/e2fsprogs/prebuilt/host/bin ../../build/ohos/images/mkimage

echo "update eng_system.img in packages/phone/images"
../../build/ohos/images/build_image.py --depfile gen/build/ohos/images/phone_eng_system_image.d --image-name eng_system --input-path packages/phone/eng_system --image-config-file ../../build/ohos/images/mkimage/eng_system_image_conf.txt --device-image-config-file packages/imagesconf/eng_system_image_conf.txt --output-image packages/phone/images/eng_system.img --target-cpu arm --build-variant root --build-image-tools-path clang_x64/thirdparty/e2fsprogs clang_x64/thirdparty/f2fs-tools ../../third_party/e2fsprogs/prebuilt/host/bin ../../build/ohos/images/mkimage

echo "update sys_prod.img in packages/phone/images"
../../build/ohos/images/build_image.py --depfile gen/build/ohos/images/phone_sys_prod_image.d --image-name sys_prod --input-path packages/phone/sys_prod --image-config-file ../../build/ohos/images/mkimage/sys_prod_image_conf.txt --device-image-config-file packages/imagesconf/sys_prod_image_conf.txt --output-image packages/phone/images/sys_prod.img --target-cpu arm --build-variant root --build-image-tools-path clang_x64/thirdparty/e2fsprogs clang_x64/thirdparty/f2fs-tools ../../third_party/e2fsprogs/prebuilt/host/bin ../../build/ohos/images/mkimage

echo "update system.img in packages/phone/images"
../../build/ohos/images/build_image.py --depfile gen/build/ohos/images/phone_system_image.d --image-name system --input-path packages/phone/system --image-config-file ../../build/ohos/images/mkimage/system_image_conf.txt --device-image-config-file packages/imagesconf/system_image_conf.txt --output-image packages/phone/images/system.img --target-cpu arm --build-variant root --build-image-tools-path clang_x64/thirdparty/e2fsprogs clang_x64/thirdparty/f2fs-tools ../../third_party/e2fsprogs/prebuilt/host/bin ../../build/ohos/images/mkimage

echo "update ramdisk.img in packages/phone/images"
../../build/ohos/images/build_image.py --depfile gen/build/ohos/images/phone_ramdisk_image.d --image-name ramdisk --input-path packages/phone/ramdisk --image-config-file ../../build/ohos/images/mkimage/ramdisk_image_conf.txt --device-image-config-file packages/imagesconf/ramdisk_image_conf.txt --output-image ramdisk.img --target-cpu arm --build-variant root --build-image-tools-path clang_x64/thirdparty/e2fsprogs clang_x64/thirdparty/f2fs-tools ../../third_party/e2fsprogs/prebuilt/host/bin ../../build/ohos/images/mkimage

echo "update updater_ramdisk.img in packages/phone/images"
../../build/ohos/images/build_image.py --depfile gen/build/ohos/images/phone_updater_ramdisk_image.d --image-name updater_ramdisk --input-path packages/phone/updater --image-config-file ../../build/ohos/images/mkimage/updater_ramdisk_image_conf.txt --device-image-config-file packages/imagesconf/updater_ramdisk_image_conf.txt --output-image updater_ramdisk.img --target-cpu arm --build-variant root --build-image-tools-path clang_x64/thirdparty/e2fsprogs clang_x64/thirdparty/f2fs-tools ../../third_party/e2fsprogs/prebuilt/host/bin ../../build/ohos/images/mkimage

echo "update userdata.img in packages/phone/images"
../../build/ohos/images/build_image.py --depfile gen/build/ohos/images/phone_userdata_image.d --image-name userdata --input-path packages/phone/data --image-config-file ../../build/ohos/images/mkimage/userdata_image_conf.txt --device-image-config-file packages/imagesconf/userdata_image_conf.txt --output-image packages/phone/images/userdata.img --target-cpu arm --build-variant root --build-image-tools-path clang_x64/thirdparty/e2fsprogs clang_x64/thirdparty/f2fs-tools ../../third_party/e2fsprogs/prebuilt/host/bin ../../build/ohos/images/mkimage

echo "update vendor.img in packages/phone/images"
../../build/ohos/images/build_image.py --depfile gen/build/ohos/images/phone_vendor_image.d --image-name vendor --input-path packages/phone/vendor --image-config-file ../../build/ohos/images/mkimage/vendor_image_conf.txt --device-image-config-file packages/imagesconf/vendor_image_conf.txt --output-image packages/phone/images/vendor.img --target-cpu arm --build-variant root --build-image-tools-path clang_x64/thirdparty/e2fsprogs clang_x64/thirdparty/f2fs-tools ../../third_party/e2fsprogs/prebuilt/host/bin ../../build/ohos/images/mkimage

echo "The third step finished successfully"

cd ../../

